package array.simple;

public class BuddyStrings_859 {

    public boolean buddyStrings(String s, String goal) {
        if (s.length() != goal.length()) {
            return false;
        }
        int sum = 0;
        int[] cn1 = new int[26], cn2 = new int[26];
        for (int i = 0; i < s.length(); i++) {
            int i1 = s.charAt(i) - 'a', i2 = goal.charAt(i) - 'a';
            cn1[i1]++;cn2[i2]++;
            if (i1 != i2)
                sum++;
        }
        boolean flag = false;
        for (int i = 0; i < 26; i++) {
            if (cn1[i] != cn2[i])
                return false;
            if (cn1[i] > 1)
                flag = true;
        }
        return sum == 2 || (sum == 0 && flag);

    }

    public static void main(String[] args) {
        BuddyStrings_859 bs = new BuddyStrings_859();
        System.out.println(bs.buddyStrings("aaaaaaabc", "aaaaaaacb"));
    }
}
